﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;


namespace QuanLyQuanCafe
{
    public partial class FrmDenBuThietHai : Form
    {
        public FrmDenBuThietHai()
        {
            InitializeComponent();
        }
        private HoaDonBL hoadonBL = new HoaDonBL();
        private CT_DenBuBL ctdenbuBL = new CT_DenBuBL();
        private string mahd;
        private float tongtien = 0;
        private float sotiencu = 0;
        private void FrmDenBuThietHai_Load(object sender, EventArgs e)
        {
            cbBan.DisplayMember = "MaBan";
            cbBan.ValueMember = "MaBan";
            cbBan.DataSource = hoadonBL.BanChuaTinhTien();
        }


        private void cbBan_SelectedIndexChanged(object sender, EventArgs e)
        {
            mahd = hoadonBL.GetMaHD(cbBan.SelectedValue.ToString());
            DataTable dt = ctdenbuBL.DSCT_DenBu(mahd);
            if (dt.Rows.Count > 0)
            {
                tbSoTien.Text = dt.Rows[0]["SoTien"].ToString();
                tbLyDo.Text = dt.Rows[0]["LyDo"].ToString();
            }
            else
            {
                tbSoTien.Text = "0";
                tbLyDo.Text = "";
            }
            sotiencu = float.Parse(tbSoTien.Text);
        }
        private CT_DenBuBO LayThongTinDB()
        {
            CT_DenBuBO ctdenbuBO = new CT_DenBuBO();
            ctdenbuBO.MaHD = mahd;
            ctdenbuBO.SoTien = float.Parse(tbSoTien.Text);
            ctdenbuBO.LyDo = tbLyDo.Text;

            return ctdenbuBO;
        }
        private void btLuu_Click(object sender, EventArgs e)
        {
            
            if (cbBan.Text!="")
            {
                mahd = hoadonBL.GetMaHD(cbBan.SelectedValue.ToString());
                tongtien = hoadonBL.GetTongTien(mahd);
                DataTable dt = ctdenbuBL.DSCT_DenBu(mahd);
                CT_DenBuBO ctdenbuBO = LayThongTinDB();
                if (dt.Rows.Count > 0)
                {
                    if (ctdenbuBL.SuaDenBu(ctdenbuBO))
                    {
                        tongtien = tongtien + float.Parse(tbSoTien.Text) - sotiencu;
                        hoadonBL.SuaTongTien(tongtien, mahd);
                    }
                    
                    Close();
                }
                else
                {
                    if (ctdenbuBL.ThemDenBu(ctdenbuBO))
                    {
                        hoadonBL.SuaTongTien(tongtien, mahd);
                    }
                    Close();
                }
            }
            else MessageBox.Show("Bạn Chưa Chọn Bàn Cần Đền Bù!","Quản Lý Quán Café", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void btHuy_Click(object sender, EventArgs e)
        {
            Close();
        }


    }
}
